Inside Macintosh: Files

Previous | Chapter Top | Chapter Contents | Next

Reading, Writing, and Closing Files

You can use the functions FSRead , FSWrite , and FSClose to read data from a file, write data to a file, and close an open file. All three of these functions operate on open files. You can use any one of a variety of routines to open a file (for example, FSpOpenDF ).

FSRead

You can use the FSRead function to read any number of bytes from an open file.

FUNCTION FSRead (refNum: Integer; VAR count: LongInt;
                                         buffPtr: Ptr): OSErr;
refNum
The file reference number of an open file.
count
On input, the number of bytes to read; on output, the number of bytes actually read.
buffPtr
A pointer to the data buffer into which the bytes are to be read.

DESCRIPTION

The FSRead function attempts to read the requested number of bytes from the specified file into the specified buffer. The buffPtr parameter points to that buffer; this buffer is allocated by your application and must be at least as large as the count parameter.

Because the read operation begins at the current mark, you might want to set the mark first by calling the SetFPos function. If you try to read past the logical end-of-file, FSRead reads in all the data up to the end-of-file, moves the mark to the end-of-file, and returns eofErr as its function result. Otherwise, FSRead moves the file mark to the byte following the last byte read and returns noErr .

RESULT CODES

noErr

0

No error

ioErr

-36

I/O error

fnOpnErr

-38

File not open

eofErr

-39

Logical end-of-file reached

posErr

-40

Attempt to position mark before start of file

fLckdErr

-45

File is locked

paramErr

-50

Negative count

rfNumErr

-51

Bad reference number

afpAccessDenied

-5000

User does not have the correct access to the file

FSWrite

You can use the FSWrite function to write any number of bytes to an open file.

FUNCTION FSWrite (refNum: Integer; VAR count: LongInt;
                                         buffPtr: Ptr): OSErr;
refNum
The file reference number of an open file.
count
On input, the number of bytes to write to the file; on output, the number of bytes actually written.
buffPtr
A pointer to the data buffer from which the bytes are to be written.

DESCRIPTION

The FSWrite function takes the specified number of bytes from the specified data buffer and attempts to write them to the specified file. Because the write operation begins at the current mark, you might want to set the mark first by calling the SetFPos function.

If the write operation completes successfully, FSWrite moves the file mark to the byte following the last byte written and returns noErr . If you try to write past the logical end-of-file, FSWrite moves the logical end-of-file. If you try to write past the physical end-of-file, FSWrite adds one or more clumps to the file and moves the physical end-of-file accordingly.

RESULT CODES

noErr

0

No error

dskFulErr

-34

Disk full

ioErr

-36

I/O error

fnOpnErr

-38

File not open

posErr

-40

Attempt to position mark before start of file

wPrErr

-44

Hardware volume lock

fLckdErr

-45

File is locked

vLckdErr

-46

Software volume lock

paramErr

-50

Negative count

rfNumErr

-51

Bad reference number

wrPermErr

-61

Read/write permission doesn't allow writing

FSClose

You can use the FSClose function to close an open file.

FUNCTION FSClose (refNum: Integer): OSErr;
refNum
The file reference number of an open file.

DESCRIPTION

The FSClose function removes the access path for the specified file and writes the contents of the volume buffer to the volume.

The FSClose function calls PBFlushFile internally to write the file's bytes onto the volume. To ensure that the file's catalog entry is updated, you should call FlushVol after you call FSClose .<36pt>

Make sure that you do not call FSClose with a file reference number of a file that has already been closed. Attempting to close the same file twice may result in loss of data on a volume. See the description of file control blocks in the chapter "File Manager" in this book for a discussion of how this can happen.<36pt>

RESULT CODES

noErr

0

No error

ioErr

-36

I/O error

fnOpnErr

-38

File not open

fnfErr

-43

File not found

rfNumErr

-51

Bad reference number


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next